package com.hiwan.system.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.hiwan.platform.utils.DateUtils;
import com.hiwan.system.common.persistence.BasePojo;
import com.hiwan.system.util.UserCacheUtils;

/**
 * 
 * Datetime   ： 2016年1月6日 下午3:24:00<br>
 * Title      :  PojoExtraInit.java<br>
 * Description:   初始化BasePojo类中的属性<br>
 * Company    :  hiwan<br>
 * @author cbj
 *
 */
@Aspect
@Component
public class PojoExtraPropInitAspect {
	
	private static final String BEFRE_INSERT = "execution(* com.hiwan.system.common.base.GenericService+.add(..)) && execution(* com.hiwan.system.service..*.add(..))";
	private static final String BEFRE_UPDATE = "execution(* com.hiwan.system.common.base.GenericService+.modify(..)) && execution(* com.hiwan.system.service..*.modify(..))";

	@Before(BEFRE_INSERT)
	public void beforeInertAdvice(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		Object arg = args[0];
		if (!(arg instanceof BasePojo))
			return;
		BasePojo bp = (BasePojo) arg;
		bp.setCreateBy(UserCacheUtils.getUser().getUserId());
		bp.setCreateDate(DateUtils.getDate());
	}

	@Before(BEFRE_UPDATE)
	public void beforeUpdateAdvice(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		Object arg = args[0];
		if (!(arg instanceof BasePojo))
			return;
		BasePojo bp = (BasePojo) arg;
		bp.setUpdateBy(UserCacheUtils.getUser().getUserId());
		bp.setUpdateDate(DateUtils.getDate());
	}
}
